package com.ymq.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ymq.reggie.common.BaseContext;
import com.ymq.reggie.common.R;
import com.ymq.reggie.dto.DishDto;
import com.ymq.reggie.dto.OrdersDto;
import com.ymq.reggie.entity.OrderDetail;
import com.ymq.reggie.entity.Orders;
import com.ymq.reggie.entity.ShoppingCart;
import com.ymq.reggie.service.OrdersServise;
import com.ymq.reggie.service.OrederDetailService;
import com.ymq.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrdersServise ordersServise;
    @Autowired
    private OrederDetailService orederDetailService;
    @PostMapping("/submit")
    public R<String> sumb(@RequestBody Orders orders){
        log.info("订单信息：{}",orders);
      ordersServise.submit(orders);


        return R.success("支付成功");
    }

    /**
     * 通过订单号查找详细菜品
     * @param id
     * @return
     */
    public List<OrderDetail> getOrderDetailListByOderid(Long id){
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,id);
        orderDetailLambdaQueryWrapper.orderByDesc(OrderDetail::getAmount);
        List<OrderDetail> list = orederDetailService.list(orderDetailLambdaQueryWrapper);
        return list;


    }

    //最新订单页面
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        //分页查询构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<OrdersDto> ordersPage1 = new Page<>(page,pageSize);
        //通过用户id拿到所有订单信息
        Long userId = BaseContext.getThreadLocalId();
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId,userId);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        ordersServise.page(ordersPage,ordersLambdaQueryWrapper);
        //orders-details查询
        LambdaQueryWrapper<OrdersDto> ordersDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            Long orderid = item.getId();
            List<OrderDetail> orderDetailListByOderid = this.getOrderDetailListByOderid(orderid);
            BeanUtils.copyProperties(item, ordersDto);
            ordersDto.setOrderDetails(orderDetailListByOderid);
            return ordersDto;
        }).collect(Collectors.toList());
         // 拷贝
         BeanUtils.copyProperties(ordersPage,ordersPage1,"records");
        Page<OrdersDto> ordersDtoPage = ordersPage1.setRecords(list);


        return R.success(ordersDtoPage);
    }

    /**
     * 订单分页及查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime){
       log.info("begin{} end time{}",beginTime,endTime);
        //分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //订单号查询  //时间查询
        ordersLambdaQueryWrapper.like(number!=null,Orders::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime).
                lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);


        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        ordersServise.page(ordersPage,ordersLambdaQueryWrapper);

        ordersServise.page(ordersPage,ordersLambdaQueryWrapper);

        return R.success(ordersPage);

    }

    /**
     * 订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<Orders> sent(@RequestBody Orders orders){
        Long id = orders.getId();
//        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        ordersLambdaQueryWrapper.eq(i)
        Orders orders1 = ordersServise.getById(id);
        orders1.setStatus(orders.getStatus());
        ordersServise.saveOrUpdate(orders1);
        return R.success(orders1);

    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){

        Long id = orders.getId();
        Orders orders1 = ordersServise.getById(id);
        Orders orders2 = new Orders();
//        BeanUtils.copyProperties(orders1,orders2);
//        ordersServise.save(orders);
        //清空购物车
        shoppingCartService.cleanShoppingCatr();
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> list = orederDetailService.list(orderDetailLambdaQueryWrapper);

        //拷贝
       // BeanUtils.copyProperties(list,"orderId"shoppingCartList,);
        List<ShoppingCart> shoppingCartList=shoppingCartList=list.stream().map((item)->{
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(item,shoppingCart);
            Long threadLocalId = BaseContext.getThreadLocalId();
            shoppingCart.setUserId(threadLocalId);
            return shoppingCart;
        }).collect(Collectors.toList());
  
        shoppingCartService.saveBatch(shoppingCartList);



        return R.success("下单成功");
    }

    

}
